home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14464 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  40 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Recognizing class instantiation type?
  5. Date: 31 Mar 1996 19:27:47 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4jmmbj$ojt@arl-news-svc-5.compuserve.com>
  8. NNTP-Posting-Host: hd80-162.compuserve.com
  9.  
  10. danubius@coho.halcyon.com () s'Θcrit :
  11. > Is there any way to determine inside of a class whether it was
  12. > instantiated in the dynamic memory (with "new"), or on the stack?
  13. > Thanks,
  14. > Joe
  15. Apparently no.
  16. But I see what you mean: do you have to delete the object
  17. or not ?
  18. This object deletion should only be done in the class or
  19. function that allocated it using the new operator.
  20. It's up to you to build your classes so that you can track
  21. dynamically allocated objects. Such object pointers should be
  22. always stored in a variable prior to using them, so that
  23. future destruction is possible using the same pointer.
  24.  
  25. It may be deleted later, but then you'll have to register
  26. this pointer in another class instance or in a container
  27. so that deleting this other instance or container will
  28. delete these objects in a appropriate way.
  29.  
  30. For all other, let the compiler do the stuff for calling destructors
  31. on local and static instances...
  32.  
  33. So you must not rely on where objects are allocated to know
  34. if the object has to be deleted or not.
  35. If this is critical, you'll have to create a placement new
  36. operator which registers the new object within a container
  37. specified by the placement.
  38.  
  39.